﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace NENAData.NENAMember
{
    public class TcXml
    {

        public int GetUserId(string Email)
        {
            NENAData.NENAMember.TcMembership MembershipApi = new NENAData.NENAMember.TcMembership();

            //XElement root = XElement.Load(MembershipApi.GetUser(Email));

            XDocument xdoc = XDocument.Parse(MembershipApi.GetUser(Email));
            //XDocument root = XDocument.Load(xdoc);

            //XElement User = xdoc.Element("User");
            //int UserId = (int)User.Element("Id"); 

            var Response = xdoc.Element("Response");

            var Users = Response.Element("Users");
            var User = Users.Element("User");
            return (int)User.Element("Id");

        }

        public List<TelligentCommunity.Roles> GetUserRoles(int UserId)
        {
            NENAData.NENAMember.TcMembership MembershipApi = new NENAData.NENAMember.TcMembership();

            //XElement root = XElement.Load(MembershipApi.GetUser(Email));

            XDocument xdoc = XDocument.Parse(MembershipApi.GetUserRoles(UserId));
            //XDocument root = XDocument.Load(xdoc);

            //XElement User = xdoc.Element("User");
            //int UserId = (int)User.Element("Id"); 
            IEnumerable<string> roles = from doc in xdoc.Descendants("Role")
                          select doc.Attribute("Name").Value;
            
            List<TelligentCommunity.Roles> UserRoles = new List<TelligentCommunity.Roles>();

            foreach (var role in roles)
            {
                if (role == TelligentCommunity.Roles.ENABC_Membership.ToString())
                    UserRoles.Add(TelligentCommunity.Roles.ENABC_Membership);

                if (role == TelligentCommunity.Roles.ENIG_Membership.ToString())
                    UserRoles.Add(TelligentCommunity.Roles.ENIG_Membership);

                if (role == TelligentCommunity.Roles.SENG_Membership.ToString())
                    UserRoles.Add(TelligentCommunity.Roles.SENG_Membership);

                if (role == TelligentCommunity.Roles.EDNA_Membership.ToString())
                    UserRoles.Add(TelligentCommunity.Roles.EDNA_Membership);

                if (role == TelligentCommunity.Roles.ENAO_Membership.ToString())
                    UserRoles.Add(TelligentCommunity.Roles.ENAO_Membership);

                if (role == TelligentCommunity.Roles.NLENA_Membership.ToString())
                    UserRoles.Add(TelligentCommunity.Roles.NLENA_Membership);

                if (role == TelligentCommunity.Roles.NSENA_Membership.ToString())
                    UserRoles.Add(TelligentCommunity.Roles.NSENA_Membership);

                if (role == TelligentCommunity.Roles.NLENA_Membership.ToString())
                    UserRoles.Add(TelligentCommunity.Roles.NLENA_Membership);

                if (role == TelligentCommunity.Roles.PEIENA_Membership.ToString())
                    UserRoles.Add(TelligentCommunity.Roles.PEIENA_Membership);

                if (role == TelligentCommunity.Roles.MembershipViewAll.ToString())
                    UserRoles.Add(TelligentCommunity.Roles.MembershipViewAll);

                if (role == TelligentCommunity.Roles.MembershipAdministrator.ToString())
                    UserRoles.Add(TelligentCommunity.Roles.MembershipAdministrator);
            }

            return UserRoles;
            //return xdoc.ToString();

        }
    }
}
